/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package com.xsm.lite.event;

import com.xsm.lite.util.ToStringBuilder;


/**
 * Indicates that a RemoteRequestEvent failed without obtaining any RemoteResponseEvent.
 * i.e. there was an unexpected failure of the network or the processing service.
 * 
 * Not meant to be extended.
 * 
 * @author Sony Mathew
 */
public final class RemoteFailureEvent extends Event {

    public interface Listener extends Event.Listener {
        public void onEvent(RemoteFailureEvent e);
    }
    
    private final RemoteRequestEvent requestEvent;
    private final Throwable error;
    
    /**
     * Give the error that occured.
     * 
     * author Sony Mathew
     */
    public RemoteFailureEvent(RemoteRequestEvent requestEvent, Throwable error) {
        this.requestEvent = requestEvent;
        this.error = error;
    }
    
    /**
     * Obtain the error that occurred.
     * 
     * author Sony Mathew
     */
    public Throwable getCause() {
        return error;
    }
    
    /**
     * Recursively goes into the cause to find the deepest underlying cause.
     * 
     * author Sony Mathew
     */
    public Throwable getRootCause() {
        Throwable cause = getCause();
        if (cause != null) {
            while (cause.getCause() != null) {
                cause = cause.getCause();
            }
        }
        return cause;
    }
    
    /**
     * Provide the original request that failed.
     * 
     * author Sony Mathew
     */
    public RemoteRequestEvent getRequest() {
        return requestEvent;
    }
    
    /**
     * Indicate whether the given event was the requestor.
     *  
     * author Sony Mathew
     */
    public boolean isRequestor(RemoteRequestEvent e) {
        return getRequest() == e;
    }
    
    /**
     * Indicate whether the given event was the NOT the requestor.
     *  
     * author Sony Mathew
     */
    public boolean isNotRequestor(RemoteRequestEvent e) {
        return !isRequestor(e);
    }
         
    @Override
    public void fire(Event.Listener l) {
        ((RemoteFailureEvent.Listener)l).onEvent(this);
    }

    /**
     * author Sony Mathew
     */
    public String toString() {
        return new ToStringBuilder(getClass())
            .append("request", getRequest())
            .append("error", getRootCause())
            .toString()
            ;
    }
}
